﻿///
/// Pamela.Logic.Imps
///
namespace Pamela.Logic.Imps.Base
{
    using System;
    using System.Data.Entity.Design.PluralizationServices;
    using System.Globalization;
    using System.Linq;
    using System.Reflection;

    using Pamela.Logic.Configurations;
    using Pamela.Logic.Helpers;

    public abstract class BaseBuilder
    {
        protected Assembly assembly;
        protected BuilderInfo builderInfo;
        protected string templatePath;
        protected string layerName;

        protected PluralizationService pluralizationService = PluralizationService.CreateService(new CultureInfo("en-US"));

        public BaseBuilder(Assembly assembly, BuilderInfo info, string layerName,  string templateFile = null)
        {
            this.assembly = assembly;
            this.builderInfo = info;
            this.layerName = layerName;

            if (templateFile == null)
            {
                this.templatePath = string.Empty;
            }
            else
            {
                this.templatePath = SourceDirectory + templateFile;
            }
        }

        public abstract void Build();

        protected string RemoveId(string name)
        {
            var str = name.Substring(name.Count() - 2);

            if (str.ToLower() == "id")
            {
                return name.Substring(0, name.Count() - 2);
            }

            return name;
        }

        protected bool IsPrimaryKey(string name)
        {
            if (name.ToLower() == "id")
            {
                return true;
            }

            if (name.ToLower() == (this.builderInfo.SelectedModel + "id").ToLower())
            {
                return true;
            }

            return false;
        }

        protected bool IsForeignKey(string name)
        {
            if (IsPrimaryKey(name))
            {
                return false;
            }
            else
            {
                string str = name.ToLower().Substring(name.Count() - 2);

                if (str == "id")
                {
                    return true;
                }

                return false;
            }
        }

        protected string SourceDirectory
        {
            get
            {
                return Config.GetTemplatesPath() + "\\";
            }
        }
    }
}
